package com.ruoyi.ctcx.service.impl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.ctcx.domain.TbInsuranceAttachmentRelate;
import com.ruoyi.ctcx.service.ITbInsuranceAttachmentRelateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.ctcx.mapper.TbInsuranceAttachmentMapper;
import com.ruoyi.ctcx.domain.TbInsuranceAttachment;
import com.ruoyi.ctcx.service.ITbInsuranceAttachmentService;

/**
 * 保险附件Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-25
 */
@Service
public class TbInsuranceAttachmentServiceImpl implements ITbInsuranceAttachmentService 
{
    @Autowired
    private TbInsuranceAttachmentMapper tbInsuranceAttachmentMapper;

    @Autowired
    private ITbInsuranceAttachmentRelateService tbInsuranceAttachmentRelateService;

    /**
     * 查询保险附件
     * 
     * @param id 保险附件主键
     * @return 保险附件
     */
    @Override
    public TbInsuranceAttachment selectTbInsuranceAttachmentById(String id)
    {
        return tbInsuranceAttachmentMapper.selectTbInsuranceAttachmentById(id);
    }

    /**
     * 查询保险附件列表
     * 
     * @param tbInsuranceAttachment 保险附件
     * @return 保险附件
     */
    @Override
    public List<TbInsuranceAttachment> selectTbInsuranceAttachmentList(TbInsuranceAttachment tbInsuranceAttachment)
    {
        return tbInsuranceAttachmentMapper.selectTbInsuranceAttachmentList(tbInsuranceAttachment);
    }

    /**
     * 新增保险附件
     * 
     * @param tbInsuranceAttachment 保险附件
     * @return 结果
     */
    @Override
    public int insertTbInsuranceAttachment(TbInsuranceAttachment tbInsuranceAttachment)
    {
        tbInsuranceAttachment.setCreateTime(DateUtils.getNowDate());
        return tbInsuranceAttachmentMapper.insertTbInsuranceAttachment(tbInsuranceAttachment);
    }

    /**
     * 修改保险附件
     * 
     * @param tbInsuranceAttachment 保险附件
     * @return 结果
     */
    @Override
    public int updateTbInsuranceAttachment(TbInsuranceAttachment tbInsuranceAttachment)
    {
        tbInsuranceAttachment.setUpdateTime(DateUtils.getNowDate());
        return tbInsuranceAttachmentMapper.updateTbInsuranceAttachment(tbInsuranceAttachment);
    }

    /**
     * 批量删除保险附件
     * 
     * @param ids 需要删除的保险附件主键
     * @return 结果
     */
    @Override
    public int deleteTbInsuranceAttachmentByIds(String[] ids)
    {
        return tbInsuranceAttachmentMapper.deleteTbInsuranceAttachmentByIds(ids);
    }

    /**
     * 删除保险附件信息
     * 
     * @param id 保险附件主键
     * @return 结果
     */
    @Override
    public int deleteTbInsuranceAttachmentById(String id)
    {
        return tbInsuranceAttachmentMapper.deleteTbInsuranceAttachmentById(id);
    }


    /**
     * 根据保险信息获取附件信息
     * @param id
     * @return
     */
    @Override
    public List<TbInsuranceAttachment> selectTbInsuranceAttachmentByMainId(Long id) {
        return tbInsuranceAttachmentMapper.selectTbInsuranceAttachmentByMainId(id);
    }

    /**
     * 保存附件和关联关系
     * @param mainId
     * @param tbInsuranceAttachmentList
     */
    @Override
    public void saveAttachment(String mainId, List<TbInsuranceAttachment> tbInsuranceAttachmentList,String userName) {
        //删除已有关联关系
        tbInsuranceAttachmentRelateService.deleteRelateByMainId(mainId);

        //保存附件
        for (TbInsuranceAttachment tbInsuranceAttachment:tbInsuranceAttachmentList)
        {
            //附件id不为空，修改附件信息
            if (null != tbInsuranceAttachment.getId() && !"".equals(tbInsuranceAttachment.getId()))
            {

                tbInsuranceAttachment.setUpdateBy(userName);
                tbInsuranceAttachmentMapper.updateTbInsuranceAttachment(tbInsuranceAttachment);
            }
            //id为空，新增
            else {
                tbInsuranceAttachment.setCreateBy(userName);
                tbInsuranceAttachmentMapper.insertTbInsuranceAttachment(tbInsuranceAttachment);
            }
            //保存关联关系
            TbInsuranceAttachmentRelate tbInsuranceAttachmentRelate = new TbInsuranceAttachmentRelate();
            tbInsuranceAttachmentRelate.setMainId(mainId);
            tbInsuranceAttachmentRelate.setFileId(tbInsuranceAttachment.getId());
            tbInsuranceAttachmentRelate.setCreateBy(userName);
            tbInsuranceAttachmentRelateService.insertTbInsuranceAttachmentRelate(tbInsuranceAttachmentRelate);
        }
    }
}
